xref: /OK3568_Linux_fs/kernel/drivers/thermal/kirkwood_thermal.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0-only
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun  * Kirkwood thermal sensor driver
4*4882a593Smuzhiyun  *
5*4882a593Smuzhiyun  * Copyright (C) 2012 Nobuhiro Iwamatsu <iwamatsu@nigauri.org>
6*4882a593Smuzhiyun  */
7*4882a593Smuzhiyun #include <linux/device.h>
8*4882a593Smuzhiyun #include <linux/err.h>
9*4882a593Smuzhiyun #include <linux/io.h>
10*4882a593Smuzhiyun #include <linux/kernel.h>
11*4882a593Smuzhiyun #include <linux/of.h>
12*4882a593Smuzhiyun #include <linux/module.h>
13*4882a593Smuzhiyun #include <linux/platform_device.h>
14*4882a593Smuzhiyun #include <linux/thermal.h>
15*4882a593Smuzhiyun 
16*4882a593Smuzhiyun #define KIRKWOOD_THERMAL_VALID_OFFSET	9
17*4882a593Smuzhiyun #define KIRKWOOD_THERMAL_VALID_MASK	0x1
18*4882a593Smuzhiyun #define KIRKWOOD_THERMAL_TEMP_OFFSET	10
19*4882a593Smuzhiyun #define KIRKWOOD_THERMAL_TEMP_MASK	0x1FF
20*4882a593Smuzhiyun 
21*4882a593Smuzhiyun /* Kirkwood Thermal Sensor Dev Structure */
22*4882a593Smuzhiyun struct kirkwood_thermal_priv {
23*4882a593Smuzhiyun 	void __iomem *sensor;
24*4882a593Smuzhiyun };
25*4882a593Smuzhiyun 
kirkwood_get_temp(struct thermal_zone_device * thermal,int * temp)26*4882a593Smuzhiyun static int kirkwood_get_temp(struct thermal_zone_device *thermal,
27*4882a593Smuzhiyun 			  int *temp)
28*4882a593Smuzhiyun {
29*4882a593Smuzhiyun 	unsigned long reg;
30*4882a593Smuzhiyun 	struct kirkwood_thermal_priv *priv = thermal->devdata;
31*4882a593Smuzhiyun 
32*4882a593Smuzhiyun 	reg = readl_relaxed(priv->sensor);
33*4882a593Smuzhiyun 
34*4882a593Smuzhiyun 	/* Valid check */
35*4882a593Smuzhiyun 	if (!((reg >> KIRKWOOD_THERMAL_VALID_OFFSET) &
36*4882a593Smuzhiyun 	    KIRKWOOD_THERMAL_VALID_MASK)) {
37*4882a593Smuzhiyun 		dev_err(&thermal->device,
38*4882a593Smuzhiyun 			"Temperature sensor reading not valid\n");
39*4882a593Smuzhiyun 		return -EIO;
40*4882a593Smuzhiyun 	}
41*4882a593Smuzhiyun 
42*4882a593Smuzhiyun 	/*
43*4882a593Smuzhiyun 	 * Calculate temperature. According to Marvell internal
44*4882a593Smuzhiyun 	 * documentation the formula for this is:
45*4882a593Smuzhiyun 	 * Celsius = (322-reg)/1.3625
46*4882a593Smuzhiyun 	 */
47*4882a593Smuzhiyun 	reg = (reg >> KIRKWOOD_THERMAL_TEMP_OFFSET) &
48*4882a593Smuzhiyun 		KIRKWOOD_THERMAL_TEMP_MASK;
49*4882a593Smuzhiyun 	*temp = ((3220000000UL - (10000000UL * reg)) / 13625);
50*4882a593Smuzhiyun 
51*4882a593Smuzhiyun 	return 0;
52*4882a593Smuzhiyun }
53*4882a593Smuzhiyun 
54*4882a593Smuzhiyun static struct thermal_zone_device_ops ops = {
55*4882a593Smuzhiyun 	.get_temp = kirkwood_get_temp,
56*4882a593Smuzhiyun };
57*4882a593Smuzhiyun 
58*4882a593Smuzhiyun static const struct of_device_id kirkwood_thermal_id_table[] = {
59*4882a593Smuzhiyun 	{ .compatible = "marvell,kirkwood-thermal" },
60*4882a593Smuzhiyun 	{}
61*4882a593Smuzhiyun };
62*4882a593Smuzhiyun 
kirkwood_thermal_probe(struct platform_device * pdev)63*4882a593Smuzhiyun static int kirkwood_thermal_probe(struct platform_device *pdev)
64*4882a593Smuzhiyun {
65*4882a593Smuzhiyun 	struct thermal_zone_device *thermal = NULL;
66*4882a593Smuzhiyun 	struct kirkwood_thermal_priv *priv;
67*4882a593Smuzhiyun 	struct resource *res;
68*4882a593Smuzhiyun 	int ret;
69*4882a593Smuzhiyun 
70*4882a593Smuzhiyun 	priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
71*4882a593Smuzhiyun 	if (!priv)
72*4882a593Smuzhiyun 		return -ENOMEM;
73*4882a593Smuzhiyun 
74*4882a593Smuzhiyun 	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
75*4882a593Smuzhiyun 	priv->sensor = devm_ioremap_resource(&pdev->dev, res);
76*4882a593Smuzhiyun 	if (IS_ERR(priv->sensor))
77*4882a593Smuzhiyun 		return PTR_ERR(priv->sensor);
78*4882a593Smuzhiyun 
79*4882a593Smuzhiyun 	thermal = thermal_zone_device_register("kirkwood_thermal", 0, 0,
80*4882a593Smuzhiyun 					       priv, &ops, NULL, 0, 0);
81*4882a593Smuzhiyun 	if (IS_ERR(thermal)) {
82*4882a593Smuzhiyun 		dev_err(&pdev->dev,
83*4882a593Smuzhiyun 			"Failed to register thermal zone device\n");
84*4882a593Smuzhiyun 		return PTR_ERR(thermal);
85*4882a593Smuzhiyun 	}
86*4882a593Smuzhiyun 	ret = thermal_zone_device_enable(thermal);
87*4882a593Smuzhiyun 	if (ret) {
88*4882a593Smuzhiyun 		thermal_zone_device_unregister(thermal);
89*4882a593Smuzhiyun 		dev_err(&pdev->dev, "Failed to enable thermal zone device\n");
90*4882a593Smuzhiyun 		return ret;
91*4882a593Smuzhiyun 	}
92*4882a593Smuzhiyun 
93*4882a593Smuzhiyun 	platform_set_drvdata(pdev, thermal);
94*4882a593Smuzhiyun 
95*4882a593Smuzhiyun 	return 0;
96*4882a593Smuzhiyun }
97*4882a593Smuzhiyun 
kirkwood_thermal_exit(struct platform_device * pdev)98*4882a593Smuzhiyun static int kirkwood_thermal_exit(struct platform_device *pdev)
99*4882a593Smuzhiyun {
100*4882a593Smuzhiyun 	struct thermal_zone_device *kirkwood_thermal =
101*4882a593Smuzhiyun 		platform_get_drvdata(pdev);
102*4882a593Smuzhiyun 
103*4882a593Smuzhiyun 	thermal_zone_device_unregister(kirkwood_thermal);
104*4882a593Smuzhiyun 
105*4882a593Smuzhiyun 	return 0;
106*4882a593Smuzhiyun }
107*4882a593Smuzhiyun 
108*4882a593Smuzhiyun MODULE_DEVICE_TABLE(of, kirkwood_thermal_id_table);
109*4882a593Smuzhiyun 
110*4882a593Smuzhiyun static struct platform_driver kirkwood_thermal_driver = {
111*4882a593Smuzhiyun 	.probe = kirkwood_thermal_probe,
112*4882a593Smuzhiyun 	.remove = kirkwood_thermal_exit,
113*4882a593Smuzhiyun 	.driver = {
114*4882a593Smuzhiyun 		.name = "kirkwood_thermal",
115*4882a593Smuzhiyun 		.of_match_table = kirkwood_thermal_id_table,
116*4882a593Smuzhiyun 	},
117*4882a593Smuzhiyun };
118*4882a593Smuzhiyun 
119*4882a593Smuzhiyun module_platform_driver(kirkwood_thermal_driver);
120*4882a593Smuzhiyun 
121*4882a593Smuzhiyun MODULE_AUTHOR("Nobuhiro Iwamatsu <iwamatsu@nigauri.org>");
122*4882a593Smuzhiyun MODULE_DESCRIPTION("kirkwood thermal driver");
123*4882a593Smuzhiyun MODULE_LICENSE("GPL");
124