1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Dove thermal sensor driver
4  *
5  * Copyright (C) 2013 Andrew Lunn <andrew@lunn.ch>
6  */
7 #include <linux/device.h>
8 #include <linux/err.h>
9 #include <linux/io.h>
10 #include <linux/kernel.h>
11 #include <linux/of.h>
12 #include <linux/module.h>
13 #include <linux/platform_device.h>
14 #include <linux/thermal.h>
15 
16 #define DOVE_THERMAL_TEMP_OFFSET	1
17 #define DOVE_THERMAL_TEMP_MASK		0x1FF
18 
19 /* Dove Thermal Manager Control and Status Register */
20 #define PMU_TM_DISABLE_OFFS		0
21 #define PMU_TM_DISABLE_MASK		(0x1 << PMU_TM_DISABLE_OFFS)
22 
23 /* Dove Theraml Diode Control 0 Register */
24 #define PMU_TDC0_SW_RST_MASK		(0x1 << 1)
25 #define PMU_TDC0_SEL_VCAL_OFFS		5
26 #define PMU_TDC0_SEL_VCAL_MASK		(0x3 << PMU_TDC0_SEL_VCAL_OFFS)
27 #define PMU_TDC0_REF_CAL_CNT_OFFS	11
28 #define PMU_TDC0_REF_CAL_CNT_MASK	(0x1FF << PMU_TDC0_REF_CAL_CNT_OFFS)
29 #define PMU_TDC0_AVG_NUM_OFFS		25
30 #define PMU_TDC0_AVG_NUM_MASK		(0x7 << PMU_TDC0_AVG_NUM_OFFS)
31 
32 /* Dove Thermal Diode Control 1 Register */
33 #define PMU_TEMP_DIOD_CTRL1_REG		0x04
34 #define PMU_TDC1_TEMP_VALID_MASK	(0x1 << 10)
35 
36 /* Dove Thermal Sensor Dev Structure */
37 struct dove_thermal_priv {
38 	void __iomem *sensor;
39 	void __iomem *control;
40 };
41 
42 static int dove_init_sensor(const struct dove_thermal_priv *priv)
43 {
44 	u32 reg;
45 	u32 i;
46 
47 	/* Configure the Diode Control Register #0 */
48 	reg = readl_relaxed(priv->control);
49 
50 	/* Use average of 2 */
51 	reg &= ~PMU_TDC0_AVG_NUM_MASK;
52 	reg |= (0x1 << PMU_TDC0_AVG_NUM_OFFS);
53 
54 	/* Reference calibration value */
55 	reg &= ~PMU_TDC0_REF_CAL_CNT_MASK;
56 	reg |= (0x0F1 << PMU_TDC0_REF_CAL_CNT_OFFS);
57 
58 	/* Set the high level reference for calibration */
59 	reg &= ~PMU_TDC0_SEL_VCAL_MASK;
60 	reg |= (0x2 << PMU_TDC0_SEL_VCAL_OFFS);
61 	writel(reg, priv->control);
62 
63 	/* Reset the sensor */
64 	reg = readl_relaxed(priv->control);
65 	writel((reg | PMU_TDC0_SW_RST_MASK), priv->control);
66 	writel(reg, priv->control);
67 
68 	/* Enable the sensor */
69 	reg = readl_relaxed(priv->sensor);
70 	reg &= ~PMU_TM_DISABLE_MASK;
71 	writel(reg, priv->sensor);
72 
73 	/* Poll the sensor for the first reading */
74 	for (i = 0; i < 1000000; i++) {
75 		reg = readl_relaxed(priv->sensor);
76 		if (reg & DOVE_THERMAL_TEMP_MASK)
77 			break;
78 	}
79 
80 	if (i == 1000000)
81 		return -EIO;
82 
83 	return 0;
84 }
85 
86 static int dove_get_temp(struct thermal_zone_device *thermal,
87 			  int *temp)
88 {
89 	unsigned long reg;
90 	struct dove_thermal_priv *priv = thermal->devdata;
91 
92 	/* Valid check */
93 	reg = readl_relaxed(priv->control + PMU_TEMP_DIOD_CTRL1_REG);
94 	if ((reg & PMU_TDC1_TEMP_VALID_MASK) == 0x0) {
95 		dev_err(&thermal->device,
96 			"Temperature sensor reading not valid\n");
97 		return -EIO;
98 	}
99 
100 	/*
101 	 * Calculate temperature. According to Marvell internal
102 	 * documentation the formula for this is:
103 	 * Celsius = (322-reg)/1.3625
104 	 */
105 	reg = readl_relaxed(priv->sensor);
106 	reg = (reg >> DOVE_THERMAL_TEMP_OFFSET) & DOVE_THERMAL_TEMP_MASK;
107 	*temp = ((3220000000UL - (10000000UL * reg)) / 13625);
108 
109 	return 0;
110 }
111 
112 static struct thermal_zone_device_ops ops = {
113 	.get_temp = dove_get_temp,
114 };
115 
116 static const struct of_device_id dove_thermal_id_table[] = {
117 	{ .compatible = "marvell,dove-thermal" },
118 	{}
119 };
120 
121 static int dove_thermal_probe(struct platform_device *pdev)
122 {
123 	struct thermal_zone_device *thermal = NULL;
124 	struct dove_thermal_priv *priv;
125 	int ret;
126 
127 	priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
128 	if (!priv)
129 		return -ENOMEM;
130 
131 	priv->sensor = devm_platform_get_and_ioremap_resource(pdev, 0, NULL);
132 	if (IS_ERR(priv->sensor))
133 		return PTR_ERR(priv->sensor);
134 
135 	priv->control = devm_platform_get_and_ioremap_resource(pdev, 1, NULL);
136 	if (IS_ERR(priv->control))
137 		return PTR_ERR(priv->control);
138 
139 	ret = dove_init_sensor(priv);
140 	if (ret) {
141 		dev_err(&pdev->dev, "Failed to initialize sensor\n");
142 		return ret;
143 	}
144 
145 	thermal = thermal_zone_device_register("dove_thermal", 0, 0,
146 					       priv, &ops, NULL, 0, 0);
147 	if (IS_ERR(thermal)) {
148 		dev_err(&pdev->dev,
149 			"Failed to register thermal zone device\n");
150 		return PTR_ERR(thermal);
151 	}
152 
153 	ret = thermal_zone_device_enable(thermal);
154 	if (ret) {
155 		thermal_zone_device_unregister(thermal);
156 		return ret;
157 	}
158 
159 	platform_set_drvdata(pdev, thermal);
160 
161 	return 0;
162 }
163 
164 static int dove_thermal_exit(struct platform_device *pdev)
165 {
166 	struct thermal_zone_device *dove_thermal =
167 		platform_get_drvdata(pdev);
168 
169 	thermal_zone_device_unregister(dove_thermal);
170 
171 	return 0;
172 }
173 
174 MODULE_DEVICE_TABLE(of, dove_thermal_id_table);
175 
176 static struct platform_driver dove_thermal_driver = {
177 	.probe = dove_thermal_probe,
178 	.remove = dove_thermal_exit,
179 	.driver = {
180 		.name = "dove_thermal",
181 		.of_match_table = dove_thermal_id_table,
182 	},
183 };
184 
185 module_platform_driver(dove_thermal_driver);
186 
187 MODULE_AUTHOR("Andrew Lunn <andrew@lunn.ch>");
188 MODULE_DESCRIPTION("Dove thermal driver");
189 MODULE_LICENSE("GPL");
190